/*
 * AddCategoryPriceSchemeTwoForm.java
 *
 * Created on November 21, 2008, 5:42 PM
 */

package rrs;
import java.sql.*;
/**
 *
 * @author  Paul Molenda
 */
public class AddCategoryPriceSchemeTwoForm extends javax.swing.JFrame {

    /** Creates new form AddCategoryPriceSchemeTwoForm */
    public AddCategoryPriceSchemeTwoForm() {
        initComponents();
        conn = DBInterface.dbConnOpen();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblPercentDiscount = new javax.swing.JLabel();
        lblDescrip = new javax.swing.JLabel();
        lblID = new javax.swing.JLabel();
        txtID = new javax.swing.JTextField();
        txtPercentDiscount = new javax.swing.JTextField();
        txtDescrip = new javax.swing.JTextField();
        btnAdd = new javax.swing.JButton();
        lblTotalPrice = new javax.swing.JLabel();
        txtTotalPrice = new javax.swing.JTextField();
        lblmisc = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        lblPercentDiscount.setText("Percent Discount");
        lblPercentDiscount.setName("lblPercentDiscount"); // NOI18N

        lblDescrip.setText("Description");
        lblDescrip.setName("lblDescrip"); // NOI18N

        lblID.setText("ID");
        lblID.setName("lblID"); // NOI18N

        txtID.setName("txtID"); // NOI18N

        txtPercentDiscount.setName("txtPercentDiscount"); // NOI18N

        txtDescrip.setName("txtDescrip"); // NOI18N

        btnAdd.setText("ADD");
        btnAdd.setName("btnAdd"); // NOI18N
        btnAdd.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnAddMouseClicked(evt);
            }
        });

        lblTotalPrice.setText("Total Price");
        lblTotalPrice.setName("lblTotalPrice"); // NOI18N

        txtTotalPrice.setName("txtTotalPrice"); // NOI18N

        lblmisc.setName("lblmisc"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(251, Short.MAX_VALUE)
                .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(81, 81, 81))
            .addGroup(layout.createSequentialGroup()
                .addGap(45, 45, 45)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(lblDescrip)
                            .addComponent(lblID))
                        .addGap(18, 18, 18))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblPercentDiscount)
                        .addGap(6, 6, 6))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblTotalPrice)
                        .addGap(18, 18, 18)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(txtID, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtDescrip, javax.swing.GroupLayout.DEFAULT_SIZE, 209, Short.MAX_VALUE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(txtTotalPrice, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(txtPercentDiscount, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)))
                .addContainerGap(71, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addComponent(lblmisc)
                .addContainerGap(412, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblID))
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtDescrip, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblDescrip))
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblPercentDiscount)
                    .addComponent(txtPercentDiscount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtTotalPrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblTotalPrice))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 85, Short.MAX_VALUE)
                .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblmisc)
                .addGap(8, 8, 8))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btnAddMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnAddMouseClicked
 String id = txtID.getText();
 String descrip = txtDescrip.getText();
 String percentDiscount = txtPercentDiscount.getText();
 String totalPrice = txtTotalPrice.getText();
// String totalQuantity = txtTotalQuantity.getText();
// String discountQuantity = txtDiscountQuantity.getText();
  String member = "";
 if (parametersOk(id, percentDiscount,totalPrice))
 {
     member = id.substring(1, 2).toString();
      try
        {          
           Statement stmt = conn.createStatement();
           String insertStatement = "INSERT INTO CATEGORYPRICESCHEMETWO VALUES('" + id +
                                    "', " + "'" + descrip + "', " + percentDiscount + ", " +
                                    totalPrice + ", " + "'N'" + ", " + "'" + member + "'" + ")";           
           stmt.executeUpdate(insertStatement);
           conn.commit();
        } // end try
        catch (SQLException e)
        {
           lblmisc.setText("The id entered already exists.");
        } // end catch
        DBInterface.dbConnClose();
 }
}//GEN-LAST:event_btnAddMouseClicked
private boolean parametersOk(String id, String percentDiscount, String totalPrice)
{
    boolean parametersOK = false;
    if (id.length()==7)
    {
        if (id.charAt(0) == '8')
        {
            if (id.substring(1, 2).toString().toLowerCase().equals("1") || id.substring(1, 2).toString().equals("2"))
            {
                //OK the ID is good to go.
                try
                {
                    Integer testPercentDiscount = 0;
                            testPercentDiscount = testPercentDiscount.parseInt(percentDiscount);
                    Integer testTotalPrice = 0;
                            testTotalPrice = testTotalPrice.parseInt(totalPrice);                    
                    parametersOK = true;
                }
                catch (NumberFormatException e)
                {                    
                    parametersOK = false;
                }
            }
        }
    }
    return parametersOK;
}
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JLabel lblDescrip;
    private javax.swing.JLabel lblID;
    private javax.swing.JLabel lblPercentDiscount;
    private javax.swing.JLabel lblTotalPrice;
    private javax.swing.JLabel lblmisc;
    private javax.swing.JTextField txtDescrip;
    private javax.swing.JTextField txtID;
    private javax.swing.JTextField txtPercentDiscount;
    private javax.swing.JTextField txtTotalPrice;
    // End of variables declaration//GEN-END:variables
Connection conn;
}
